'''
Created on 2009-11-30

@author: Jialai_Zhu
'''
import string, os, sys  
from configure import *  
#dir = mufs_dir  
#print '----------- no sub dir'  

def listxml(dir):
    xmllist = []  
    files = os.listdir(dir)  
    for f in files:  
        x = dir + os.sep + f
        try:
            if os.path.splitext(x)[1].lower() == '.xml':
                xmllist.append(x)
        except:
            pass        
    return xmllist    
#  
#print '----------- all dir'  
#  
#for root, dirs, files in os.walk(dir):  
#    for name in files:  
#        print os.path.join(root, name)  
t = '''
<br>
<span>
<a href=/tree/tree.html?%s class="smarterwiki-linkify">
%s 
</a>%s
</span>
'''
from xml.dom import minidom                                          
def get_link(file):
    xmldoc = minidom.parse(file)
    root = xmldoc.getElementsByTagName('root')[0] 
    link = xmldoc.getElementsByTagName('link')[0]
    md5u = link.getAttribute('md5u')
    url = link.getAttribute('url')
    try:time = root.getAttribute('time')
    except: time = "----" 
    return t % (md5u , url, time)
def run():
    mufs_dir = root_dir + r'mufs'+os.sep
    ret = listxml(mufs_dir)
    out = '<html>'
    for file in ret:
        try:
            out += get_link(file)
        except Exception, ex:
            print Exception, ':', ex  
            pass
    return out + "</html>"    

#run()    
